/**
 * 
 */
package org.istt.service.impl;

import java.util.Date;
import java.util.List;

import org.istt.dao.FileDao;
import org.istt.domain.AttachFileDetail;
import org.istt.domain.Exchange;
import org.istt.domain.File;
import org.istt.domain.FileManagement;
import org.istt.model.FileView;
import org.istt.service.FileManager;
import org.istt.service.FileMgrManager;
import org.istt.web.helper.ExtendedPaginatedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Huy
 *
 */
@Service("fileManager")
public class FileManagerImpl extends ManagerImpl<File, String> implements FileManager {
	
	private FileMgrManager fileMgrManager;
	
	/**
	 * @param categoryDao the categoryDao to set
	 */
	@Autowired
	public void setFileDao(FileDao dao) {
		this.dao = dao;
	}
	
	/**
	 * @param fileMgrManager the fileMgrManager to set
	 */
	@Autowired
	public void setFileMgrManager(FileMgrManager fileMgrManager) {
		this.fileMgrManager = fileMgrManager;
	}

	@Override
	public List<File> searchByCatId(Long catId) {
		return ((FileDao)dao).findByCatId(catId);
	}

	@Override
	public ExtendedPaginatedList search(Integer type,
										Long departmentId,
										Long fileStatusId,
										Long fieldId,
										Long cateId, 
										Date fromDate, 
										Date toDate, 
										String fileId, 
										String sender, 
										ExtendedPaginatedList paginatedList) {
		List<File> results = ((FileDao)dao).search(type, departmentId, fileStatusId, fieldId, cateId, fromDate, toDate, fileId, sender, paginatedList
                .getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList
                .getSortDirection(), paginatedList.getSortCriterion());
			
        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(((FileDao)dao).getSearchRecordCount(type, departmentId, fileStatusId, fieldId, cateId, fromDate, toDate, fileId, sender));
		
		return paginatedList;
	}
	
	@Override
	public List<File> searchTrans(Long fieldId, Long cateId, String fileId, String sender) {
		if (fieldId == 0l) fieldId = null;
		if (cateId == 0l) cateId = null;
		return ((FileDao)dao).findTrans(fieldId, cateId, fileId, sender);
	}
	
	@Override
	public void remove(String id) {
		//File fl = dao.findById(id, false);
		//fl.setAttachFiles(null);
		super.remove(id);
	}
	
	@Override
	public void addFileMgrToFile(File f, FileManagement fmgr) {
		save(f);
		dao.flush();
		if (fmgr != null) {
			fmgr.setFile(f);
			fileMgrManager.save(fmgr);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#searchByFileIds(java.lang.String[])
	 */
	@Override
	public List<List<Object>> searchByFileIds(String[] fileIds) {
		return ((FileDao)dao).findByFileIds(fileIds);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#findByStatus(int, org.istt.web.helper.ExtendedPaginatedList)
	 */
	@Override
	public ExtendedPaginatedList findByStatus(int status, ExtendedPaginatedList paginatedList) {
		
		List<File> results = ((FileDao)dao).findByStatus(status, paginatedList
                .getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList
                .getSortDirection(), paginatedList.getSortCriterion());
			
        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(((FileDao)dao).findByStatusRecordsCount(status));
        
		return paginatedList;
	}
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#getExchangeByFileId(java.lang.String[])
	 */
	@Override
	public List<Exchange> getExchangeByFileId(String fileId, Long userId) {
		return ((FileDao)dao).findExchangeByFileId(fileId, userId);
	}	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#getExchangeByFileId(java.lang.String[])
	 */
	@Override
	public List<FileManagement> getFileManagementByFileId(String fileId, String username) {
		return ((FileDao)dao).findFileManagementByFileId(fileId, username);
	}

	@Override
	public List<AttachFileDetail> getAttachFileDetailByFileId(String fileId) {
		return ((FileDao)dao).getAttachFileDetailByFileId(fileId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#removeAttachFileDetail(java.lang.Long)
	 */
	@Override
	public void removeAttachFileDetail(String fileId) {
		((FileDao)dao).removeAttachFileDetail(fileId);
	}
	
	@Override
	public List<Object[]> getFileProcessVoucher(String fileId) {
		return ((FileDao)dao).getFileProcessVoucher(fileId);
	}
	
	@Override
	public List<File> getFilesForReport(Long cateId,Long subCateId, Date fromDate, Date toDate) {
		
		return ((FileDao)dao).getFilesForReport(cateId,subCateId, fromDate, toDate);
	}		

	@Override
	public List<File> getFilesByNameAndCode(String fileCode,String sender) {
		
		return ((FileDao)dao).findByCodeAndName(fileCode,sender);
	}

	@Override
	public ExtendedPaginatedList findFileByApprover(Long approverId, Date fromDate, Date toDate,String status,String filterCondition, ExtendedPaginatedList paginatedList) {
		
		List<FileView> results = ((FileDao)dao).findFileByApprover(approverId, fromDate, toDate,status,filterCondition, paginatedList
                .getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList
                .getSortDirection(), paginatedList.getSortCriterion());
			
        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(((FileDao)dao).getRecordCountByFilterCondition(approverId, fromDate, toDate,status, filterCondition));
        
//        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>TOTAL RECORD: "+((FileDao)dao).getRecordCountByStatus(approverId, fromDate, toDate, status, filterCondition));
		return paginatedList;
	}
	

	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#getFileByUser(java.lang.Long, org.joda.time.DateTime, org.joda.time.DateTime, org.istt.web.helper.ExtendedPaginatedList)
	 */
	@Override
	public ExtendedPaginatedList getFileByUser(Long userId, Date fromDate, Date toDate, ExtendedPaginatedList paginatedList) {
		List<FileManagement> results = ((FileDao)dao).getFileByUser(userId, fromDate, toDate, paginatedList
                .getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList
                .getSortDirection(), paginatedList.getSortCriterion());
			
        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(((FileDao)dao).getFileByUserRecordCount(userId, fromDate, toDate));
		return paginatedList;
	}

	@Override
	public int getTotalRecordByFilterCondition(Long approverId, Date fromDate,
			Date toDate, String status, String filterCondition) {
		return ((FileDao)dao).getRecordCountByFilterCondition(approverId, fromDate, toDate,status, filterCondition);
	}

	@Override
	public ExtendedPaginatedList findFileByDeparment(Long departmentId, String status,
			ExtendedPaginatedList paginatedList) {
		List<FileView> results = ((FileDao)dao).findFileByDepartment(departmentId,status, paginatedList
                .getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList
                .getSortDirection(), paginatedList.getSortCriterion());
			
        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(((FileDao)dao).getRecordCountByDepartment(departmentId, status));
		return paginatedList;
	}

	@Override
	public boolean checkNewFileByApprover(Long userId, Date fromDate, Date toDate, boolean status) {
		if(((FileDao)dao).checkNewFileByApprover(userId, fromDate, toDate, status) > 0) return true;
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#isAbleToNextStep(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean isAbleToNextStep(String fileId, String username) {
		return ((FileDao)dao).isAbleToNextStep(fileId, username);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#getMaxNumberNoOfFile()
	 */
	@Override
	public int getMaxNumberNoOfFile(Long fieldId, int reportStartingPoint) {
		return ((FileDao)dao).getMaxNumberNoOfFile(fieldId,reportStartingPoint);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#getNumberOfFileOn13th()
	 */
	@Override
	public int getNumberOfFileOnDateReport(Long fieldId, int reportStartingPoint) {
		return ((FileDao)dao).getNumberOfFileOnReportDate(fieldId, reportStartingPoint);
	}
	
	@Override
	public ExtendedPaginatedList getHistoryByUser(Long userId,Date from, Date to, ExtendedPaginatedList paginatedList) {
		List<FileManagement> results = ((FileDao)dao).getHistoryByUser(userId, from, to, paginatedList
                .getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList
                .getSortDirection(), paginatedList.getSortCriterion());
			
        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(((FileDao)dao).getHistoryCountByUser(userId, from, to));
		return paginatedList;
	}

	@Override
	public List<File> getReceivedFileByDate(Long deptId, Date from, Date to) {
		// TODO Auto-generated method stub
		return ((FileDao)dao).findFileByDepartmentByDate(deptId, from, to);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileManager#remove(java.lang.Long[])
	 */
	@Override
	public void remove(String[] ids) {
		((FileDao)dao).remove(ids);
	}

	@Override
	public List<AttachFileDetail> findAttachFileDetailByFileId(String fileId) {
		return ((FileDao)dao).findAttachFileDetailByFileId(fileId);
	}

	@Override
	public List<File> searchWithoutPagination(Long departmentId,
			Long fileStatusId, Long fieldId, Long cateId, Date fromDate,
			Date toDate, String fileId, String sender) {
		// TODO Auto-generated method stub
		List<File> results = ((FileDao)dao).searchWithoutPagination(departmentId, fileStatusId, fieldId, cateId, fromDate, toDate, fileId, sender);
		
		return results;
	}

	@Override
	public List<File> searchFile(Long cateId, Date fromDate, Date toDate,
			String fileId, String sender, int pageNum) {
		// TODO Auto-generated method stub
		List<File> results = ((FileDao)dao).searchFile(cateId, fromDate, toDate, fileId, sender, pageNum);
		
		return results;
	}

	@Override
	public void removeAllFile() {
		((FileDao)dao).removeAllFile();
	}

	@Override
	public void removeAllDataTable() {
		((FileDao)dao).removeAllDataTable();
	}
}
